package com.szh.dongtaijieshou;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    TestBroadcast testBroadcast;
    private Button mainSend;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainSend = (Button) findViewById(R.id.main_send);

        initReceiver();
        final Intent intent = new Intent();
        intent.putExtra("msg", "接受动态广播成功");
        intent.setAction("com.szh.dongtaijieshou");
        mainSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendBroadcast(intent);
            }
        });

    }

    private void initReceiver() {
        testBroadcast = new TestBroadcast();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.szh.dongtaijieshou");
        registerReceiver(testBroadcast,filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(testBroadcast);
    }
}
